package com.enjoy.ioc.metaData;

import com.enjoy.ioc.pojo.Student;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.annotation.Annotation;

@Component
public class AnnotationMetaData implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
        System.out.println("AnnotationMetaData被调用到了");

    }

    public void metaData() {
        CachingMetadataReaderFactory cachingMetadataReaderFactory =
                new CachingMetadataReaderFactory(this.resourceLoader);
        try {
            MetadataReader metadataReader = cachingMetadataReaderFactory.getMetadataReader(Student.class.getName());
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            System.out.println(annotationMetadata);
            for (String type : annotationMetadata.getAnnotationTypes()) {
                System.out.println("type = " + type);
            }
            for (MergedAnnotation<Annotation> annotation : annotationMetadata.getAnnotations()) {
                System.out.println("annotation.getType() = " + annotation.getType());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
